/*
<<<<<<< HEAD
 * @Author: pao
 * @Date:   2017-01-01 20:47:17
 * @Last Modified by:   pao
 * @Last Modified time: 2017-01-05 12:12:41
 */

'use strict';

const fs = require('fs');
const xtpl = require('xtpl');
const orm = require('orm');
const bodyParser = require('body-parser');


exports.getVideoListPage = (req, res) => {
    const keyword = req.query.keyword || '';
    //计算数据条数
    req.models.video.count({ vtitle: orm.like("%" + keyword + "%") }, (err, count) => {
        //每页条数
        const pageSize = parseInt(req.query.pageSize) || 2;
        //页面索引
        const pageIndex = req.query.pageIndex * pageSize;
        //总页数
        const totalPage = Math.ceil(count / pageSize);

        const currentPage = parseInt(req.query.pageIndex) || 0;

        //定义一个数组保存给分页按钮
        const pages = [];
        for (let i = 0; i < totalPage; i++) {
            pages.push(i);
        }
        //获取视频列表页，先到数据库中查找目标数据  
        req.models.video.find({ vtitle: orm.like("%" + keyword + "%") }, { offset: pageIndex, limit: pageSize }, (err, videos) => {
            if (err) {
                console.log(err);
                return;
            }
            //将获取的数据结合页面模板返回给前台
            xtpl.renderFile('./view/list.html', { uname: req.session.uname, keyword: keyword, currentPage: currentPage, totalPage: totalPage, video: videos, list: pages, pageSize: pageSize }, (err, content) => {
                res.setHeader('Content-Type', 'text/html;charset=utf-8');
                res.end(content);
            })
        })
    })
}

exports.getAddPage = (req, res) => {
    xtpl.renderFile('./view/add.html', { uname: req.session.uname }, (err, content) => {
        if (err) {
            console.log(err);
            return;
        }
        res.end(content);
    })
}

exports.add = (req, res) => {
    const video = req.body;
    req.models.video.create([{
        vtitle: video.vtitle,
        vsortno: video.vsortno,
        vvideoid: video.vvideoid,
        vsummary: video.vsummary,
        vimgurl: video.vimgurl,
        vremark: video.vremark
    }], (err) => {
        if (err) {
            console.log(err);
            return;
        }
        res.end("<script>alert('增加成功');window.location='/video/list'</script>")
    })
}

exports.getEditPage = (req, res) => {
    //通过pathname传值的后台通过req.params获取
    const vid = req.params.vid;
    req.models.video.get(vid, (err, video) => {
        //通过id查找对应的数据，并填充到要返回给前台的页面
        video.uname = req.session.uname;
        xtpl.renderFile('./view/edit.html', video, (err, content) => {
            if (err) {
                console.log(err);
                return;
            }
            res.end(content);
        })
    })
}

exports.saveEdit = (req, res) => {
    const data = req.body;
    const vid = req.params.vid;
    req.models.video.get(vid, (err, video) => {
        video.vtitle = data.vtitle;
        video.vsortno = data.vsortno;
        video.vvideoid = data.vvideoid;
        video.vsummary = data.vsummary;
        video.vimgurl = data.vimgurl;
        video.vremark = data.vremark;
        video.save((err) => {
            res.end("<script>alert('保存成功');window.location='/video/list'</script>")

        })
    });
}

exports.delVideo = (req, res) => {

    const vid = req.params.vid;

    const returnObj = { state: 0, msg: "删除未成功,请重新删除" }
    req.models.video.get(vid, (err, video) => {
        if (err) {
            console.log(err);
            return;
        };
        returnObj.state = 1;
        returnObj.msg = "删除成功";
        video.remove((err) => {
            if (err) {
                console.log(err);
                return;
            }
        });
        res.end(JSON.stringify(returnObj));
    })

}
